/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmAddAnimal.java
 *
 * Created on 19/09/2012, 22:27:50
 */
package br.com.petshop.dao.frames;

import br.com.petshop.dao.AnimalDAO;
import br.com.petshop.dao.ClienteDAO;
import br.com.petshop.entidades.Animal;
import br.com.petshop.entidades.AnimalType;
import br.com.petshop.entidades.Cliente;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author sidney
 */
public class frmAddAnimal extends javax.swing.JDialog {

    private final AnimalDAO dao;

    /** Creates new form frmAddAnimal */
    public frmAddAnimal(java.awt.Frame parent, boolean modal, AnimalDAO dao) {
        super(parent, modal);
        this.dao = dao;
        initComponents();
        loadInitialComboData();
        tf_Nome.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        panelComponents = new javax.swing.JPanel();
        jl_Nome = new javax.swing.JLabel();
        jl_Raca = new javax.swing.JLabel();
        jl_Idade = new javax.swing.JLabel();
        jl_Dono = new javax.swing.JLabel();
        jl_Tipo = new javax.swing.JLabel();
        tf_Nome = new javax.swing.JTextField();
        tf_Raca = new javax.swing.JTextField();
        tf_Idade = new javax.swing.JTextField();
        jComboBox_Dono = new javax.swing.JComboBox();
        jComboBox_Tipo = new javax.swing.JComboBox();
        jb_Add = new javax.swing.JButton();
        jb_Canc = new javax.swing.JButton();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar Animal");
        setResizable(false);

        panelComponents.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jl_Nome.setText("Nome:");

        jl_Raca.setText("Raça:");

        jl_Idade.setText("Idade:");

        jl_Dono.setText("Dono:");

        jl_Tipo.setText("Tipo:");

        jComboBox_Dono.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jComboBox_Tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jb_Add.setText("Adicionar");
        jb_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_AddActionPerformed(evt);
            }
        });

        jb_Canc.setText("Cancelar");
        jb_Canc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_CancActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelComponentsLayout = new javax.swing.GroupLayout(panelComponents);
        panelComponents.setLayout(panelComponentsLayout);
        panelComponentsLayout.setHorizontalGroup(
            panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panelComponentsLayout.createSequentialGroup()
                        .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jl_Nome)
                            .addComponent(jl_Raca))
                        .addGap(19, 19, 19)
                        .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelComponentsLayout.createSequentialGroup()
                                .addComponent(tf_Raca, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jl_Idade)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_Idade, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jl_Tipo)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBox_Tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(panelComponentsLayout.createSequentialGroup()
                        .addComponent(jl_Dono)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox_Dono, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(14, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelComponentsLayout.createSequentialGroup()
                .addContainerGap(192, Short.MAX_VALUE)
                .addComponent(jb_Add)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jb_Canc)
                .addContainerGap())
        );
        panelComponentsLayout.setVerticalGroup(
            panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jl_Nome, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_Nome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jl_Raca, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_Raca, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Idade, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_Idade, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Tipo, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox_Tipo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jl_Dono, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox_Dono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelComponentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_Add)
                    .addComponent(jb_Canc))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelComponents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelComponents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-402)/2, (screenSize.height-217)/2, 402, 217);
    }// </editor-fold>//GEN-END:initComponents

    private void jb_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_AddActionPerformed
        //verifica se o campo nome foi preenchido
        if ("".equals(tf_Nome.getText())) {
            JOptionPane.showMessageDialog(this, "Campo nome é obrigatório!");
            tf_Nome.requestFocus();
            return;
        }
        int resp = JOptionPane.showConfirmDialog(this, "Confirma?", "Inclusão de Novo Animal", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);
        if (resp != 0)return;
        
        Animal animal = new Animal();
        try {
            animal.setNome(tf_Nome.getText());
            animal.setRaca(tf_Raca.getText());
            if (!tf_Idade.getText().equals("")) {
                animal.setIdade(Integer.parseInt(tf_Idade.getText()));
            } else {
                animal.setIdade(0);
            }
            animal.setTipo((AnimalType) jComboBox_Tipo.getSelectedItem());//permite configurar o tipo na lista de tipos do combo
            animal.setIdCliente((Cliente) jComboBox_Dono.getSelectedItem());//permite configurar o cliente na lista de clientes do combo

            dao.adicionarAnimal(animal);
            setVisible(false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Erro ao Adicionar Animal!");
            Logger.getLogger("Erro: " + ex);
            return;
        }

    }//GEN-LAST:event_jb_AddActionPerformed

    private void jb_CancActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_CancActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jb_CancActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmAddAnimal dialog = new frmAddAnimal(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox_Dono;
    private javax.swing.JComboBox jComboBox_Tipo;
    private javax.swing.JButton jb_Add;
    private javax.swing.JButton jb_Canc;
    private javax.swing.JLabel jl_Dono;
    private javax.swing.JLabel jl_Idade;
    private javax.swing.JLabel jl_Nome;
    private javax.swing.JLabel jl_Raca;
    private javax.swing.JLabel jl_Tipo;
    private javax.swing.JPanel panelComponents;
    private javax.swing.JTextField tf_Idade;
    private javax.swing.JTextField tf_Nome;
    private javax.swing.JTextField tf_Raca;
    // End of variables declaration//GEN-END:variables

    //precarga dos combo boxes 
    private void loadInitialComboData() {

        //carrega os tipos possiveis de animais atendidos no pet shop
        DefaultComboBoxModel cbm = new DefaultComboBoxModel(AnimalType.values());
        jComboBox_Tipo.setModel(cbm);
        //carrega a lista dos donos dos animais
        try {
            ClienteDAO cliDAO = new ClienteDAO();
            Vector<Cliente> vetorCliente = new Vector<Cliente>(cliDAO.getTodosOsClientes());
            DefaultComboBoxModel cbmCliente = new DefaultComboBoxModel(vetorCliente);
            jComboBox_Dono.setModel(cbmCliente);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }
    }
}
